package org.fansea;

import java.util.HashMap;

import static java.lang.Thread.sleep;

/**
 * @Projectname: HandTornQuestions
 * @Filename: Queue
 * @Author: FANSEA
 * @Date:2024/10/27 21:56
 */
public class ArrayQueue<T> {
    private final T[] arr;
//    private int head;
    private int tail;

    public ArrayQueue(int size) {
        arr = (T[]) new Object[size];
    }
    // 入队
    public void enqueue(T t) {
        arr[tail++] = t;
    }

    // 出队
    public T dequeue() {
        if(tail == 0) {
            return null;
        }
        T t = arr[0];
        //首位后面的元素开始都向前移动一位
        for(int i = 1; i < tail; i++) {
            arr[i-1] = arr[i];
        }
        arr[tail--] = null;
        return t;
    }

    public synchronized T take() throws InterruptedException {
        while (tail == 0) {
            wait();
        }
        T t = dequeue();
        notifyAll();
        return t;
    }

    public synchronized void push(T t) throws InterruptedException {
        if (tail == arr.length) {
            wait();
        }
        enqueue(t);
        notifyAll();
    }

    public static void main(String[] args) {
        HashMap<Object, Object> objectObjectHashMap = new HashMap<>(5);
        objectObjectHashMap.put("1","1");
        System.out.println(objectObjectHashMap.size());
    }
}
